package com.cn.qgx428.orderingmeal.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cn.qgx428.orderingmeal.R;
import com.cn.qgx428.orderingmeal.Store;
import com.cn.qgx428.orderingmeal.myClass.CollectionShop;
import com.cn.qgx428.orderingmeal.myClass.MyKeyInfo;
import com.cn.qgx428.orderingmeal.myClass.Shop;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by QinX on 2017/4/6.
 */

public class CollectionShopAdapter extends RecyclerView.Adapter<CollectionShopAdapter.CollectionShopViewHolder> {
    private MyKeyInfo keyInfo;
    private static final String TAG = "CollectionShopAdapter";

    private Context context;
    private List<CollectionShop> shops;

    public CollectionShopAdapter(Context context, List<CollectionShop> shops, MyKeyInfo keyInfo){
        this.context = context;
        this.shops = shops;
        this.keyInfo = keyInfo;
    }

    @Override
    public CollectionShopViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View viewItem = LayoutInflater.from(context).inflate(R.layout.collection_shop_item, parent, false);
        CollectionShopViewHolder viewHolder = new CollectionShopViewHolder(viewItem);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(CollectionShopViewHolder holder, int position) {
        final Shop shop = shops.get(position).getShop();
        Log.d(TAG, "组合"+shop.getName());
        holder.name.setText(shop.getName());
        Picasso.with(context).load(shop.getImg().getFileUrl()).fit().into(holder.shop_pic);
        holder.shopLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                keyInfo.setNowShopID(shop.getObjectId());
                keyInfo.setNowPicUrl(shop.getImg().getFileUrl());
                keyInfo.setNowShopName(shop.getName());
                keyInfo.setShopGrade(shop.getGrade());
                keyInfo.setShopPhone(shop.getPhone());
                Intent intent = new Intent(context, Store.class);
                intent.putExtra("keyInfo",keyInfo);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return shops.size();
    }

    public class CollectionShopViewHolder extends RecyclerView.ViewHolder {

        private TextView name;
        private ImageView shop_pic;
        private LinearLayout shopLayout;

        public CollectionShopViewHolder(View itemView) {
            super(itemView);

            name = (TextView) itemView.findViewById(R.id.collection_shop_name);
            shop_pic = (ImageView) itemView.findViewById(R.id.colletction_collect_shop_img);
            shopLayout = (LinearLayout) itemView.findViewById(R.id.collection_shopLayout);
        }
    }


}
